// Resize title img
function resizeTitleImg(){
  document.getElementById("titleimg").style.height = document.body.clientHeight/3;
  document.getElementById("titleimg").style.width = document.body.clientWidth - 500;
  //document.getElementById("search").style.marginTop = document.body.clientHeight/100;
}

Date.prototype.shortFormat = function(){
  return (this.getMonth()+1)+"/"+this.getDate()+"/"+this.getFullYear();
};

// Blog object constructor
function Blog(body,date,image) {
  // Assign the properties
  this.body = body;
  this.date = date;
  this.image = image;
}
// Return a string representation of the blog entry
Blog.prototype.toString = function(){
  return "["+this.date.shortFormat()+"]"+this.body;
};

// Return a formatted HTML representation of the blog entry
Blog.prototype.toHTML = function(highlight){
  // Use a gray background as a highlight, if specified
  var blogHTML="";
  blogHTML += highlight ? "<p style='background-color:#EEEEEE'>" : "<p>";
  // Generate the formatted blog HTML code
  if (this.image){
    blogHTML += "<strong>"+this.date.shortFormat()+"</strong><br /><table><tr><td><img src='"+this.image+"'/></td><td style='vertical-align:top'>"+this.body+"</td></tr></table><em>"+this.signature+"</em></p>";
  } else{
    blogHTML += "<strong>"+this.date.shortFormat()+"</strong><br />"+this.body+"<br /><em>"+this.signature+"</em></p>";
  }
  return blogHTML;
};

//  See if the blog body contains a string of text
Blog.prototype.containsText = function(text){
  return ((this.body.toLowerCase().indexOf(text.toLowerCase())) != -1);
};

Blog.prototype.signature = "Bi XiChao";

Blog.showSignature = function(){
  alert("This blog created by " + Blog.prototype.signature+".");
}

Blog.blogSorter = function(blog1,blog2){
  return blog2.date-blog1.date;
}

// Global array of blog entries
var blog = [
  new Blog("The accident happened just hours after a Taliban suicide car bomber struck a British forces convoy in Kabul. Officials said three civilians were wounded in the attack, but no fatalities were reported.",new Date("08/14/2008")),
  new Blog("Taliban spokesman Zabihullah Mujahid emailed a detailed statement about the attack to VOA. He said the suicide car bombing was carried out in response to air raids by foreign troops in different parts of Afghanistan, including Kunduz, which killed Afghan civilians, including doctors.",new Date("08/19/2008")),
  new Blog("He claimed that at least 12 people were killed in the bombing Sunday, though the militant group is known for exaggerating the death tolls in similar attacks.",new Date("08/16/2008")),
  new Blog("Taliban insurgents overran Kunduz in a stunning assault in late September, prompting a massive counter-offensive by Afghan security forces backed by U.S. airstrikes that retook most of the city.",new Date("08/21/2008")),
  new Blog("One of the air raids on October 3 mistakenly struck a hospital in Kunduz run by Docors Without Borders, known by its French acronym, MSF, according to U.S. military and civilian officials.",new Date("09/01/2008")),
  new Blog("Monkey News (originally Chimpanzee That) was a regular feature of the Xfm radio shows, and some podcasts, in which Karl re-tells a story he has read about a monkey or an ape. The stories are usually very loosely based on a factual report he has read on the Internet where monkeys (or more often, chimpanzees) have done something very unusual or human-like.",new Date("05/18/2007"),"monkeys.jpg"),
  new Blog("It is said that RAMONA VAN COURT JONES and GERALDINE DODGE wouldn't let it rain at their dog shows sadly both ladies, with all their powers are gone, and only the unpredictable weather is still with us. It wouldn't be the first Montgomery weekend to be rained on and so we take it in stride.",new Date("04/24/2015"),"Dogs.jpg"),
  new Blog("The AFL advised that the Eagles would be in line to receive a second-round draft pick - currently No.37 - after they decided not to match the Cats' offer for the restricted free agent.",new Date("08/18/2008"),"Cats.jpg")
];

function test(){
  alert("Testing");
}

// Show the list of blog entries
function showBlog(numEntries){
  // First sort the blog in revers chronological order(most recent first)
  blog.sort(Blog.blogSorter);
  // Adjust the number of entries to show the full blog, if necessary
  if (!numEntries)
    numEntries = blog.length;
  // Show the blog entries
  var i=0;
  var blogText="";
  while (i<blog.length && i<numEntries) {
    // Use a gray background for every other blog entry
    // Generate the formatted blog HTML code
    blogText += blog[i].toHTML(i%2 == 0);
    i++;
  }
  // Set the blog HTML code on the page
  document.getElementById("blog").innerHTML = blogText;
}

function searchBlog() {
  var searchText = document.getElementById("searchtext").value;
  document.getElementById("blog").innerHTML="";
  blog.sort(function(blog1,blog2){return blog2.date-blog1.date;});
  for (var i=0;i<blog.length;i++){
    // See if the blog entry contains the search text
    if (blog[i].containsText(searchText)){
      //  Change the blog content
      document.getElementById("blog").innerHTML += blog[i].toHTML(0);
    }
  }
  // If the search text was't found, display a message
  if (document.getElementById("blog").innerHTML == ""){
    document.getElementById("blog").innerHTML = "<p>Sorry, there are on blog entries containg the search text.</p>";
  }
}

function randomBlog(){
  // Pick a random number between 0 and blog.length-1
  var i = Math.floor(Math.random()*blog.length);
  alert(blog[i]);
}


window.onload = function(){
  showBlog(5);
  resizeTitleImg();
  document.getElementById("showall").onclick = function(){
    showBlog();
  }
  window.onresize = resizeTitleImg;
  document.getElementById("search").onclick = searchBlog;
  document.getElementById("randomblog").onclick = randomBlog;
  document.getElementById("showauthor").onclick = Blog.showSignature;
}
